package com.headwind.logistics.logistics.controller;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.Customer;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.logistics.dto.LogisticsIntegralTypeDTO;
import com.headwind.logistics.logistics.pojo.LogisticsIntegral;
import com.headwind.logistics.logistics.pojo.LogisticsIntegralMall;
import com.headwind.logistics.logistics.service.intl.LogisticsIntegralServiceIntl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 小恶魔
 * @Date 2020-08-02 13:49
 * @Version 1.0
 * @DESCRIPTION 顾客财富控制器
 */
@Controller
@RequestMapping("/logistics/customer")
public class CustomerFortuneController {

    //积分业务逻辑层
    @Autowired
    private LogisticsIntegralServiceIntl logisticsIntegralServiceIntl;

    //api统一响应接口
    @Autowired
    private ResultGenerator resultGenerator;

    /**
     * 通过顾客id获取积分详情
     * @param session
     * @return
     */
    @GetMapping("queryIntegralDetail")
    @ResponseBody
    public RestResult queryIntegralDetailByCustomerId(HttpSession session){
        //session获取顾客对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<LogisticsIntegralTypeDTO> logisticsIntegralTypeDTOS =
                logisticsIntegralServiceIntl.queryIntgralDetailByCustomerId(customer.getId());

        return resultGenerator.getSuccessResult(logisticsIntegralTypeDTOS);
    }

    /**
     * 获取全部积分商城商品
     * @return
     */
    @GetMapping("queryAllIntegralMall")
    @ResponseBody
    public RestResult queryAllIntegralMall(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        //session获取客户对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));
        List<LogisticsIntegralMall> logisticsIntegralMalls = logisticsIntegralServiceIntl.queryAllIntegralMall();

        map.put("data",logisticsIntegralMalls);
        map.put("integral",customer.getIntegral());

        return resultGenerator.getSuccessResult(map);
    }

    /**
     * 积分商城兑换
     * @param logisticsIntegral
     * @param integralMall
     * @param session
     * @return
     */
    @PostMapping("integralExchange")
    @ResponseBody
    public RestResult integralExchange(LogisticsIntegral logisticsIntegral, LogisticsIntegralMall integralMall, HttpSession session){
        //从session中获得顾客对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));
        Integer count = logisticsIntegralServiceIntl.integralExchange(logisticsIntegral, integralMall, customer.getId());

        if (count > 0 ){

            customer.setIntegral(customer.getIntegral() - integralMall.getIntegralPrice());

            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("兑换失败，重新试试");
    }

}
